/**
 * @filename:SupplierContractController 2022-03-22 02:40:49
 * @project com/lwl/blue  V1.0
 * Copyright(c) 2020 <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a> Co. Ltd.
 * All right reserved.
 */
package com.lwl.blue.controller;

import com.alibaba.excel.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwl.blue.commont.base.*;
import com.lwl.blue.commont.utils.UserContextUtil;
import com.lwl.blue.entity.Contract;
import com.lwl.blue.entity.ContractDetail;
import com.lwl.blue.entity.SupplierContract;
import com.lwl.blue.entity.SupplierContractDetail;
import com.lwl.blue.service.ContractDetailService;
import com.lwl.blue.service.ContractService;
import com.lwl.blue.service.SupplierContractDetailService;
import com.lwl.blue.service.SupplierContractService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>自定义方法写在这里</p>
 *
 * <p>说明： 工厂合同API接口层</P>
 *
 * @version: V1.0
 * @author: <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a>
 */
@Api(tags = "工厂合同")
@RestController
@RequestMapping("/supplierContract")
@Slf4j
public class SupplierContractController extends BaseController {
    @Autowired
    SupplierContractService supplierContractService;
    @Autowired
    SupplierContractDetailService supplierContractDetailService;



    @PostMapping("/pages")
    public PageResult<SupplierContract> getPages(@RequestBody PageParam<SupplierContract> pageParam) {
        JsonResult<Page<SupplierContract>> pages = supplierContractService.pages(pageParam);
        PageResult<SupplierContract> re = new PageResult<>();
        re.success(pages.getData().getTotal(), pages.getData().getRecords());
        return re;
    }

    @PostMapping("saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody @Valid SupplierContract contract) {
        setInsetOrUpdateUser(contract);
        QueryWrapper<SupplierContract> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(SupplierContract::getContractNo, contract.getContractNo())
                .eq(SupplierContract::getIsDelete, 0);
        if (null != contract.getId()) {
            wrapper.lambda().notIn(SupplierContract::getId, contract.getId());
        }
        int count = supplierContractService.count(wrapper);
        if (count > 0) {
            throw new BizException(ErrorCode.DATA_EXISTS_ERROR.getCode(), ErrorCode.DATA_EXISTS_ERROR.getMsg() + contract.getContractNo());
        }
        supplierContractService.saveOrUpdate(contract);
        return new JsonResult().success("请求成功");
    }

    @PostMapping("/delete/{id}")
    public JsonResult saveOrUpdate(@PathVariable(value = "id") Integer id) {
        supplierContractService.update(new UpdateWrapper<SupplierContract>().lambda()
                .set(SupplierContract::getIsDelete, Const.IS_DELETE_YES)
                .set(SupplierContract::getUpdateUser, UserContextUtil.getUser().getUserName())
                .eq(SupplierContract::getId, id));

        supplierContractDetailService.update(new UpdateWrapper<SupplierContractDetail>().lambda()
                .set(SupplierContractDetail::getIsDelete, Const.IS_DELETE_YES)
                .set(SupplierContractDetail::getUpdateUser, UserContextUtil.getUser().getUserName())
                .eq(SupplierContractDetail::getSupplierContractId, id));
        return new JsonResult().success("请求成功");
    }

    @PostMapping("/select")
    public JsonResult select() {
        List<SupplierContract> reList = supplierContractService.list(new QueryWrapper<SupplierContract>()
                .lambda().eq(SupplierContract::getIsDelete, 0)
                .orderByDesc(SupplierContract::getId));
        return new JsonResult().success(reList);
    }

    @GetMapping("/createSupplierContract/{id}")
    public void createSupplierContract(HttpServletResponse response, @PathVariable(value = "id") Integer id) {
        supplierContractService.createSupplierContract(response, id);
    }

    @PostMapping("/createByContract/{contractId}/{supplierId}")
    public JsonResult createByContractId(@PathVariable(value = "contractId") Integer contractId, @PathVariable(value = "supplierId") Integer supplierId) {
        return supplierContractService.createByContractId(contractId,supplierId);
    }
}
